﻿using UnityEditor;
using UnityEngine;

/* -----------------------------------------------------------------------------------------
 * MiniKimmy菜单命令:
 * 初始化打包环境
 * 打包所有AB资源&Lua脚本到资源文件夹
 * 生成files.txt
 * (不常用)清除无法手动清除的AB标签.
 ------------------------------------------------------------------------------------------*/

namespace XLua_Ki
{
    public class MiniKimmyMenu
    {
        /// 创建该游戏的资源根目录文件夹.
        [MenuItem("XLua/Minikimmy/Init Env", priority = 11)]
        public static void Initialize()
        {
            if (AppConst.GameName == null) return;

            Util.ResumeFiles(Util.GameDataPath(AppConst.GameName), true, true);
            Debug.Log("Init Finish...");
        }


        /// 删掉整个游戏在StreamingAssets里的所有资源.
        [MenuItem("XLua/Minikimmy/CLEAR ALL RESOURCE", priority = 12)]
        public static void DeleteAllResourcesData()
        {
            if (AppConst.GameName == null) return;

            Util.ResumeFiles(Util.GameDataPath(AppConst.GameName), true, false);
            Debug.Log("CLEAR FINISH...");
        }

        /// 打包AB资源+Lua脚本到StreamingAssets文件夹.
        [MenuItem("XLua/Minikimmy/BuildABs To StreamingAssets", priority = 13)]
        public static void BuildResources()
        {
            if (AppConst.GameName == null) return;

            var datapath = Util.GameDataPath(AppConst.GameName);
            if (!Util.ExistsFile(datapath, true))
            {
                Debug.Log("Please 'Init Env'");
            }
            else
            {
                BuildPipeline.BuildAssetBundles(datapath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);  //打包所有AB资源
                Util.BuildLuaScripts(datapath);                                                                            //打包所有Lua脚本

                Debug.Log(string.Format("打包完毕~~,到StreamingAssets/{0}", AppConst.GameName));
            }
        }

        /// 创建files.txt文件,用于对比服务器文件.
        [MenuItem("XLua/Minikimmy/Build files.txt", priority = 14)]
        public static void BuildFiles()
        {
            if (AppConst.GameName == null) return;
            string content = null;

            string filesLocalPath = Util.GameDataPath(AppConst.GameName) + "files.txt";
            Util.ResumeFiles(filesLocalPath, false, false);
            Util.GetAllFilesMD5(Util.GameDataPath(AppConst.GameName), ref content);
            Util.WriteAllText(filesLocalPath, content);
            Debug.Log("Build files.txt finished~~");
        }


        /// 若AB标签设置好但无法打包AB时(无法手动清除标签).则通过下面方法来清理(把参数'true'去掉,执行菜单即可执行该方法).
        [MenuItem("XLua/Minikimmy/Remove xxx Lable", true, priority = 16)]
        public static void RemoveABLabel()
        {
            //例如"model/minikimmy.(空后缀)"无法手动清除,则可以像用代码,点击菜单命令执行清除.
            AssetDatabase.RemoveAssetBundleName("model/minikimmy", true);
            //若是"model/minikimmy.ab"无法手动清除.如下:
            //AssetDatabase.RemoveAssetBundleName("model/minikimmy.ab", true);

            Debug.Log("remove finished...");
        }
    }
}